package com.imageim.yimirror.service.location;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.imageim.yimirror.base.BaseApplication;

/**
 * Created by Bruce.Zhou on 2018/6/7 09:44.
 * Email: 907160968@qq.com
 */

public class LocationManager {
    private static LocationManager instance;
    private LocationClient locationClient;
    public interface LocationListener {
        void onReceiveLocation(BDLocation location);
    }
    private LocationListener locationListener;

    protected LocationManager() {
        locationClient = new LocationClient(BaseApplication.getContext());
        LocationClientOption options = new LocationClientOption();
        options.setIsNeedAddress(true);
//        options.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        locationClient.setLocOption(options);
        locationClient.registerLocationListener(new BDAbstractLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                if (locationListener != null) {
                    locationListener.onReceiveLocation(bdLocation);
                }
            }
        });
    }

    public static LocationManager getInstance() {
        if (instance == null) {
//            synchronized (LocationManager.class) {
//                if (instance == null) {
                    instance = new LocationManager();
//                }
//            }
        }
        return instance;
    }

    public void start(LocationListener listener) {
        locationListener = listener;
        locationClient.start();
    }
}
